
import config from './config'
const wxApi = {
    //上传文件
    uploadFile : (filePath,name,header,formData,onProgressUpdate) =>{
        return new Promise((resolve,reject)=>{
            const uploadTask = mpvue.uploadFile({
                url : config.baseURL + '/api/upload',
                filePath:filePath,
                name:name,
                header:header,
                formData:formData,
                success:res =>{
                    resolve(res.data)
                },
                fail:err=>{
                    reject(err);
                }
            });
            if(uploadTask.onProgressUpdate){
                uploadTask.onProgressUpdate(res=>{
                    console.log('上传进度', res.progress)
                    console.log('已经上传的数据长度', res.totalBytesSent)
                    console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
                    onProgressUpdate && typeof onProgressUpdate == 'function' && onProgressUpdate(res.progress,res.totalBytesSent,res.totalBytesExpectedToSend);
                });
            }
        });
    },
    //下载文件
    downloadFile:(pathSuffix,filePath,header) =>{
        return new Promise((resolve,reject)=>{
            const dowloadFile = mpvue.downloadFile({
                url : config.baseURL + pathSuffix,
                header:header,
                filePath:filePath,
                success: res=>{
                    resolve(res);
                },
                fail : err=>{
                    reject(err);
                }
            });
        });
    },
    showToast : (title,icon,duration)=>{
        mpvue.showToast({
            title:title,
            icon:icon,
            duration:duration || 200
        });
    },
    showActionSheet : (itemList,itemColor) =>{
        return new Promise((resolve,reject)=>{
            mpvue.showActionSheet({
                itemList : itemList,
                itemColor : itemColor,
                success: res =>{
                    resolve(res);
                },
                fail : err=>{
                    reject(err);
                }
            });
        });
    },
    //获取用户当前的设置
    getSetting : () => {
        return new Promise((resolve,reject)=>{
            mpvue.getSetting({
                success : res =>{
                    resolve(res.authSetting)
                },
                fail : err =>{
                    reject(err);
                }
            });
        });
    },
    //获取用户信息
    getUserInfo :  (withCredentials = false,lang = 'zh_CN') => {
        return new Promise(async (resolve,reject)=>{
            let authSetting = await this.getSetting();
            if(authSetting && authSetting['scope.userInfo'] ){
                mpvue.getUserInfo({
                    withCredentials : withCredentials,
                    lang : lang,
                    success : res=>{
                        res.encrypt = !!withCredentials;
                        resolve(res);
                    },fail : err=>{
                        reject(err);
                    }
                });
            }else{
                reject({
                    errMsg:'用户尚未授权'
                });
            }    
        });
    }
};
export default wxApi;